package com.ruoyi.web.core.config;

import com.ruoyi.common.config.RuoYiConfig;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;

/**
 * Swagger3的接口配置
 *
 * @author ruoyi
 */
@Configuration
public class SwaggerConfig {
    /**
     * 系统基础配置
     */
    @Autowired
    private RuoYiConfig ruoyiConfig;

    /**
     * 自定义的 OpenAPI 对象
     */
    @Bean
    public OpenAPI customOpenApi() {
        return new OpenAPI()
                // 设置认证的请求头
                .components(new Components().addSecuritySchemes("apikey", securityScheme()))
//                .addSecurityItem(new SecurityRequirement().addList("apikey"))
                //添加摘要信息
                .info(new Info()
                        // 设置标题
                        .title(ruoyiConfig.getName() + "接口文档")
                        // 描述
                        .description("用于" + ruoyiConfig.getName() + "的接口文档")
                        // 作者信息
                        .contact(new Contact().name("-"))
                        // 版本
                        .version(ruoyiConfig.getVersion()));
    }

    @Bean
    public SecurityScheme securityScheme() {
        return new SecurityScheme()
                .type(SecurityScheme.Type.APIKEY)
                .name(HttpHeaders.AUTHORIZATION)
                .in(SecurityScheme.In.HEADER)
                .scheme("Bearer");
    }

    /**
     * 全局添加鉴权参数
     */
    @Bean
    public GlobalOpenApiCustomizer securityGlobalOpenApiCustomizer() {
        return openApi -> {
            if (openApi.getPaths() != null) {
                openApi.getPaths().forEach((s, pathItem) -> {
                    pathItem.readOperations().forEach(operation -> {
                        //operation.addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION));
                        operation.addSecurityItem(new SecurityRequirement().addList("apikey"));
                    });
                });
            }
        };
    }


    @Bean
    public GroupedOpenApi defaultControllersApi() {
        return GroupedOpenApi.builder()
                .group("default")
                .displayName("系统模块")
                .packagesToScan("com.ruoyi.web.controller")
                .addOpenApiMethodFilter(method -> method.getDeclaringClass().isAnnotationPresent(Tag.class))
                .build();
    }

    @Bean
    public GroupedOpenApi flowableControllersApi() {
        return GroupedOpenApi.builder()
                .group("flowable")
                .displayName("工单模块")
                .packagesToScan("com.ruoyi.flowable.controller")
                .addOpenApiMethodFilter(method -> method.getDeclaringClass().isAnnotationPresent(Tag.class))
                .build();
    }


}
